#pragma once
#include <pthread.h>

namespace MutexMoudle
{
    class Mutex
    {
    public:
        //  删除不必要的拷贝和赋值
        Mutex(const Mutex &) = delete;
        const Mutex &operator=(const Mutex &) = delete;

        Mutex()
        {
            pthread_mutex_init(&_lock, nullptr);
        }
        void Lock()
        {
            pthread_mutex_lock(&_lock);
        }
        void UnLock()
        {
            pthread_mutex_unlock(&_lock);
        }
        pthread_mutex_t* GetMuteOriginal()
        {
             return &_lock;
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_lock);
        }
        
    private:
        pthread_mutex_t _lock;
    };
    // 采用RAII风格，进行锁的管理
    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)   // 这里要保证各个线程用的是同一把锁，mutex需要定义为全局变量传给LockGuard，LockGuard在进行控制。
        :_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.UnLock();
        }

    private:
        Mutex& _mutex;
    };
}
